import type { DebugImage } from '@sentry/core';
import { debug, GLOBAL_OBJ } from '@sentry/core';
import { DEFAULT_BUNDLE_NAME } from './hermes';

/**
 * Returns debug meta images of the loaded bundle.
 */
export function getDebugMetadata(): DebugImage[] {
  if (!DEFAULT_BUNDLE_NAME) {
    return [];
  }

  const debugIdMap = GLOBAL_OBJ._sentryDebugIds;
  if (!debugIdMap) {
    return [];
  }

  const debugIdsKeys = Object.keys(debugIdMap);
  if (!debugIdsKeys.length) {
    return [];
  }

  if (debugIdsKeys.length > 1) {
    debug.warn('[Profiling] Multiple debug images found, but only one one bundle is supported. Using the first one...');
    return [];
  }

  if (!debugIdsKeys[0]) {
    return [];
  }

  const debugId = debugIdMap[debugIdsKeys[0]];
  if (!debugId) {
    return [];
  }

  return [
    {
      code_file: DEFAULT_BUNDLE_NAME,
      debug_id: debugId,
      type: 'sourcemap',
    },
  ];
}
